package com.suixun.vo;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

import com.suixun.util.Constant;

/**
 * excel导入批量跟换考勤卡的Vo
 * 
 * @author yh
 * @date 2017年12月5日
 */
public class ChangeDeviceVo implements Serializable {

    private static final long serialVersionUID = -7683294756122331720L;

    /**
     * 表头1
     */
    public static final String[] HEADER1 = { "旧卡号", "oldCardid" };

    /**
     * 表头2
     */
    public static final String[] HEADER2 = { "旧类型", "oldType" };

    /**
     * 表头3
     */
    public static final String[] HEADER3 = { "新卡号", "newCardid" };

    /**
     * 表头4
     */
    public static final String[] HEADER4 = { "新类型", "newType" };

    /**
     * 旧卡号
     */
    private String oldCardid;

    /**
     * 旧类型
     */
    private String oldType;

    /**
     * 新卡号
     */
    private String newCardid;

    /**
     * 新类型
     */
    private String newType;

    public String getOldCardid() {
        return transformCardid(this.oldCardid, this.oldType);
    }
    
    public String getProtoOldCardid() {
        return oldCardid;
    }

    public void setOldCardid(String oldCardid) {
        if (!verifyCardid(oldCardid)) {
            throw new RuntimeException("旧卡号格式错误!《旧卡号:" + oldCardid + "》");
        }
        this.oldCardid = oldCardid;
    }

    public String getOldType() {
        return oldType;
    }

    public void setOldType(String oldType) {
        if (!verifyType(oldType)) {
            throw new RuntimeException("旧类型格式错误!《旧类型:" + oldType + "》");
        }
        this.oldType = oldType;
    }

    public String getNewCardid() {
        return transformCardid(this.newCardid, this.newType);
    }
    
    public String getProtoNewCardid() {
        return newCardid;
    }

    public void setNewCardid(String newCardid) {
        if (!verifyCardid(newCardid)) {
            throw new RuntimeException("新卡号格式错误!《新卡号:" + newCardid + "》");
        }
        if (newCardid.equals(getOldCardid())) {
            throw new RuntimeException("新卡号与旧卡号重复!《新卡号:" + newCardid + "》");
        }
        this.newCardid = newCardid;
    }

    public String getNewType() {
        return newType;
    }

    public void setNewType(String newType) {
        if (!verifyType(newType)) {
            throw new RuntimeException("新类型格式错误!《新类型:" + newType + "》");
        }
        this.newType = newType;
    }

    private boolean verifyCardid(String cardid) {
        if (StringUtils.isEmpty(cardid)) {
            return false;
        }
        if (!cardid.matches("^\\d{10}$")) {
            return false;
        }
        return true;
    }

    private boolean verifyType(String type) {
        if (StringUtils.isEmpty(type)) {
            return false;
        }
        if (!type.equals(Constant.Device.WHITE_CARD1) && !type.equals(Constant.Device.WHITE_CARD2)) {
            return false;
        }
        return true;
    }
    
    private String transformCardid(String cardid, String type){
        if(type.equals(Constant.Device.WHITE_CARD1)){
            return Constant._2_4G_MDTID_PREFIX + cardid;
        }else{
            return Constant._13_56_MDTID_PREFIX + cardid;
        }
    }

}
